package com.zis.auction.web;

import com.alibaba.fastjson.JSONObject;
import com.zis.auction.model.AuctBrowhist;
import com.zis.auction.model.AuctUserFocus;
import com.zis.auction.service.IAuctBidInter;
import com.zis.auction.service.IAuctBrowhistInter;
import com.zis.auction.service.IAuctUserFocusInter;
import com.zis.auction.service.IAuctUserInter;
import com.zis.core.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2018/1/9
 */
@RestController
@RequestMapping("/api/open/auction/user")
public class AuctUserRestful {

    @Autowired
    private IAuctUserInter auctUserInter;

    @Autowired
    private IAuctBrowhistInter auctBrowhistInter;

    @Autowired
    private IAuctUserFocusInter auctUserFocusInter;

    @Autowired
    private IAuctBidInter auctBidInter;


    @RequestMapping(value = "/auth", method = RequestMethod.POST)
    @ResponseBody
    public String userAuth(@RequestBody JSONObject params) throws Exception {
        return auctUserInter.userAuth(params);
    }

    @RequestMapping(value = "/{tuId}", method = RequestMethod.GET)
    @ResponseBody
    public String getAuctUser(@PathVariable("tuId") String tuId) throws Exception {
        return auctUserInter.getAuctUser(tuId);
    }

    @RequestMapping(value = "/count/{ausId}", method = RequestMethod.GET)
    @ResponseBody
    public String countRecord(@PathVariable("ausId") String ausId) throws Exception {
        AuctBrowhist histoty = new AuctBrowhist();
        histoty.setAusId(ausId);
        int browCount = auctBrowhistInter.findByCondition(histoty).size();
        AuctUserFocus focus = new AuctUserFocus();
        focus.setAusId(ausId);
        int focusCount = auctUserFocusInter.findByCondition(focus).size();
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("brow", browCount);
        jsonObj.put("focus", focusCount);
        return ResponseEntity.createNormalJsonResponse(jsonObj);
    }

    @RequestMapping(value = "/history/list", method = RequestMethod.GET)
    @ResponseBody
    public String browList(@RequestParam String ausId, @RequestParam Integer page,
                           @RequestParam Integer pageSize) throws Exception {
        return auctBrowhistInter.findByAusId(ausId, page, pageSize);
    }

    @RequestMapping(value = "/focus/list", method = RequestMethod.GET)
    @ResponseBody
    public String focusList(@RequestParam String ausId, @RequestParam Integer page,
                           @RequestParam Integer pageSize) throws Exception {
        return auctUserFocusInter.findByAusId(ausId, page, pageSize);
    }


    @RequestMapping(value = "/bid/whether/focus", method = RequestMethod.GET)
    @ResponseBody
    public String isFocus(@RequestParam String bussId, @RequestParam String ausId) throws Exception {
        AuctUserFocus params = new AuctUserFocus();
        params.setAusId(ausId);
        params.setBussId(bussId);
        List<AuctUserFocus> focusList = auctUserFocusInter.findByCondition(params);
        if (focusList!=null&&focusList.size()>0) {
            return "ok";
        }
        return "not";
    }

    @RequestMapping(value = "/add/browsing", method = RequestMethod.GET)
    @ResponseBody
    public String addBrowsing(@RequestParam String bussId, @RequestParam String ausId) throws Exception {
        AuctBrowhist params = new AuctBrowhist();
        params.setAusId(ausId);
        params.setBussId(bussId);
        List<AuctBrowhist> browhistList = auctBrowhistInter.findByCondition(params);
        if (browhistList!=null&&browhistList.size()>0) {
            return ResponseEntity.createErrorJsonResponse("已记录");
        }
        params.setBussType("bid");
        params.setBrowTime(new Date());
        auctBrowhistInter.insert(params);
        return ResponseEntity.createNormalJsonResponse("记录完成");
    }

    @RequestMapping(value = "/bid/focus", method = RequestMethod.GET)
    @ResponseBody
    public String addFocus(@RequestParam String bussId, @RequestParam String ausId, @RequestParam String type) throws Exception {
        if (type.equals("1")) { //关注操作
            AuctUserFocus params = new AuctUserFocus();
            params.setAusId(ausId);
            params.setBussId(bussId);
            List<AuctUserFocus> focusList = auctUserFocusInter.findByCondition(params);
            if (focusList!=null&&focusList.size()>0) {
                return ResponseEntity.createNormalJsonResponse("已关注");
            }
            //添加一条关注记录
            params.setBussType("bid");
            params.setAufTime(new Date());
            auctUserFocusInter.insert(params);
            auctBidInter.updateFocus(bussId);
            return ResponseEntity.createNormalJsonResponse("关注成功");
        }
        if (type.equals("0")) { //取消关注
            auctUserFocusInter.deleteByBussAndAus(bussId, ausId);
            return ResponseEntity.createNormalJsonResponse("已取消关注");
        }
        return ResponseEntity.createErrorJsonResponse("操作错误");
    }

}
